let isLoggedIn = false;
let username = '';

function showLoginModal() {
    window.location.href = 'login.html';
}

function checkLogin() {
    if (!isLoggedIn) {
        alert('请先登录！');
        showLoginModal();
    }
}

function loginSuccess(user) {
    isLoggedIn = true;
    username = user;
    document.getElementById('loginButton').textContent = username; // 更新按钮文本为用户名
}

function updateLoginStatus() {
    if (localStorage.getItem('isLoggedIn') === 'true') {
        isLoggedIn = true;
        username = localStorage.getItem('username');
        document.getElementById('loginButton').textContent = username;
    }
}

// 页面加载时检查登录状态
window.onload = function() {
    updateLoginStatus();
    setupNavLinks();
};

function setupNavLinks() {
    document.querySelectorAll('nav a').forEach(function(link) {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            var href = this.getAttribute('href');
            if (isLoggedIn || href === '#login') {
                window.location.href = href;
            } else {
                showLoginModal();
            }
        });
    });
}

function updateLoginStatus() {
    isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
    if (isLoggedIn) {
        username = localStorage.getItem('username');
        document.getElementById('loginButton').textContent = username;
    } else {
        document.getElementById('loginButton').textContent = '请登录';
    }
}